﻿using Prism.Commands;
using System;
using System.Windows;
using System.Windows.Media;

namespace Loaf.自定义控件.Controls
{
    public class CustomWindow : Window
    {
        public static readonly DependencyProperty TitleBarColorProperty = DependencyProperty.Register(nameof(TitleBarColor), typeof(Brush), typeof(CustomWindow), new PropertyMetadata(null));
        public static readonly DependencyProperty ShowHomeButtonColorProperty = DependencyProperty.Register(nameof(ShowHomeButton), typeof(bool), typeof(CustomWindow), new PropertyMetadata(false));

        /// <summary>
        /// 是否显示主页按钮
        /// </summary>
        public bool ShowHomeButton { get => (bool)GetValue(ShowHomeButtonColorProperty); set => SetValue(ShowHomeButtonColorProperty, value); }
        /// <summary>
        /// 标题栏颜色
        /// </summary>
        public Brush TitleBarColor { get => (Brush)GetValue(TitleBarColorProperty); set => SetValue(TitleBarColorProperty, value); }
        /// <summary>
        /// 标题栏拖动事件
        /// </summary>
        public DelegateCommand TitleBarLeftClickCommand { get; set; }
        /// <summary>
        /// 窗口关闭事件
        /// </summary>
        public DelegateCommand CloseWindowCommand { get; set; }

        /// <summary>
        /// 返回主窗口事件
        /// </summary>
        public DelegateCommand HomeWindowCommand { get; set; }

        public CustomWindow()
        {
            #region 窗口初始化
            AllowsTransparency = true;
            WindowStyle = WindowStyle.None;
            ShowInTaskbar = false;
            Background = new SolidColorBrush() { Color = Colors.White, Opacity = 0.01 };
            TitleBarColor = new SolidColorBrush() { Color = Colors.Gray, Opacity = 0.05 };
            #endregion

            TitleBarLeftClickCommand = new DelegateCommand(() => { DragMove(); });
            CloseWindowCommand = new DelegateCommand(() => { Application.Current.Shutdown(); });

            Loaded += OnLoaded;
            Deactivated += OnDeactivated;
        }

        private void OnDeactivated(object? sender, EventArgs e)
        {
            this.Visibility = Visibility.Collapsed;
        }

        private void OnLoaded(object sender, RoutedEventArgs e)
        {
            this.Style = App.Current.Resources["CustomWindowStyle"] as Style;
        }
    }
}
